import {List, Flex, Button, Badge, Toast} from '@ant-design/react-native';
import React, {Component} from 'react';
import {StyleSheet, Text, View, Image, FlatList, RefreshControl, ActivityIndicator} from 'react-native';
import {connect} from 'react-redux';
import {getMsgList} from '~/api/message';
import moment from 'moment';
import {updateUnreadMsgCount} from '~/utils/backrun';
const Item = List.Item;
const mapGetter = (state) => {
    return {
        userinfo: state.userinfo,
        system: state.system,
    };
};
let pagecount = 0;
let index = 1;
let size = 10;
class MyNews extends Component {
    static navigationOptions = {
        title: '消息中心',
    };
    constructor(props) {
        super(props);
        this.state = {
            text: 2,
            list: [],
            page_index: 1,
            page_size: 10,
            isLoading: false, //加载
            refreshing: false,
            footer: 0, //上拉底部显示
        };
    }

    componentDidMount() {
        this.props.navigation.addListener('didFocus', () => {
            if (this.props.userinfo.token !== '') {
                updateUnreadMsgCount();
                this.state.page_index = 1;
                this.getList();
            }
        });
    }

    gotoinfo = (item) => {
        this.props.navigation.push('MessgeInfo', {
            msg_id: item.MessageDataID,
            view_state: item.ViewState,
            getList: () => {
                this.state.page_index = 1;
                this.getList();
            },
        });
    };

    getList = () => {
        getMsgList({
            page_index: this.state.page_index,
            page_size: this.state.page_size,
        }).then((r) => {
            if (r.code === 0) {
                this.setState({
                    list: r.data.list,
                    footer: 0,
                });
                if (this.state.page_index === 1) {
                    pagecount = r.data.total_page_count;
                    this.setState({
                        footer: r.data.total_item_count < 10 ? 2 : 0,
                    });
                }
            } else {
                Toast.info('失败', 1);
            }
        });
    };
    _onRefresh() {
        this.setState({footer: 0, page_index: index, page_size: size}, () => {
            this.getList();
        });
    }
    //上拉加载更多
    loadData = () => {
        this.setState({isLoading: false});
        let dataArray = [];
        if (pagecount - 1 > 0) {
            pagecount = pagecount - 1;
            this.setState(
                {
                    page_index: this.state.page_index + 1,
                    footer: 1,
                },
                () => {
                    getMsgList({
                        page_index: this.state.page_index,
                        page_size: this.state.page_size,
                    }).then((r) => {
                        if (r.code === 0) {
                            dataArray = r.data.list;
                            dataArray = this.state.list.concat(dataArray);
                            this.setState({
                                list: dataArray,
                                isLoading: false,
                            });
                        } else {
                            Toast.info('失败', 1);
                        }
                    });
                },
            );
        } else {
            this.setState({
                footer: 2,
            });
        }
    };

    //上拉底部显示
    genIndicator() {
        if (this.state.footer === 1) {
            return (
                <View style={styles.indicatorContainer}>
                    <ActivityIndicator style={styles.indicator} size={'large'} animating={true} />
                    <Text style={{textAlign: 'center'}}>正在加载更多</Text>
                </View>
            );
        } else if (this.state.footer === 2) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        没有更多数据了
                    </Text>
                </View>
            );
        } else if (this.state.footer === 0) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        上拉加载更多
                    </Text>
                </View>
            );
        }
    }
    _renderItem(item) {
        return (
            <Item
                style={styles.itemview}
                onPress={() => {
                    this.gotoinfo(item);
                }}>
                <Flex style={{height: 60, width: '100%'}}>
                    <Image style={{width: 50, height: 50}} source={require('~/assets/my/img2x.png')} />
                    <Flex style={{height: 60, paddingLeft: 10, paddingRight: 50, width: '100%'}} justify="center" direction="column">
                        <Flex justify="between" style={{height: 25, width: '100%'}}>
                            <Text style={{fontSize: 16}}>{item.Message}</Text>
                            <Text style={styles.itemtext}>{moment(item.ToDateTime).format('MM.DD HH:mm')}</Text>
                        </Flex>
                        <Flex justify="between" style={{height: 20, width: '100%', paddingRight: 5}}>
                            <Text numberOfLines={1} style={{fontSize: 12, color: '#666666', paddingRight: 10}}>
                                {item.Contents}
                            </Text>
                            {item.ViewState === null && <Badge dot={true} />}
                        </Flex>
                    </Flex>
                </Flex>
            </Item>
        );
    }
    render() {
        return (
            <View style={{width: '100%', height: '100%', backgroundColor: '#F6F6F6'}}>
                {this.props.userinfo.token === '' ? (
                    <Flex style={{width: '100%', height: '100%'}} justify={'center'} align={'center'}>
                        <Button
                            onPress={() => {
                                this.props.navigation.push('Login');
                            }}>
                            请登录
                        </Button>
                    </Flex>
                ) : (
                    <Flex>
                        <List style={styles.list}>
                            <FlatList
                                data={this.state.list}
                                showsVerticalScrollIndicator={false}
                                renderItem={(data) => this._renderItem(data.item)}
                                keyExtractor={(item, idx) => idx}
                                ListFooterComponent={() => this.genIndicator()} //上拉加载更多视图
                                onEndReached={() => {
                                    this.loadData();
                                }}
                                onEndReachedThreshold={0.1} // 这个属性的意思是 当滑动到距离列表内容底部不足 0.1*列表内容高度时触发onEndReached函数 为啥要加这个属性 因为不加的话滑动一点点就会立即触发onReached函数，看不到菊花加载中
                                refreshing={true}
                                refreshControl={
                                    <RefreshControl
                                        refreshing={this.state.refreshing}
                                        onRefresh={() => {
                                            this._onRefresh();
                                        }}
                                    />
                                }
                            />
                        </List>
                    </Flex>
                )}
            </View>
        );
    }
}

export default connect(mapGetter)(MyNews);

const styles = StyleSheet.create({
    scrollview: {
        flex: 1,
    },
    list: {
        width: '100%',
        flexDirection: 'column',
    },
    itemtext: {
        fontSize: 11,
        color: '#999999',
    },
    itemview: {
        width: '100%',
    },
});
